//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace Preflight.ServiceModel.Security
{
    using System;
    using System.Net.Http;

    using Preflight.Security;

    public class SwtAuthorization : IPreflightRequestAuthorization
    {
        private readonly ISwtTokenValidator validator;

        public SwtAuthorization(ISwtTokenValidator validator)
        {
            this.validator = validator;
        }

        public bool IsAuthorized(HttpRequestMessage request)
        {
            if (request == null)
            {
                Log.LogError("Null request");
                return false;
            }

            if (request.Headers.Authorization == null)
            {
                Log.LogVerbose("Authorization request not included");
                return false;
            }

            if (request.Headers.Authorization.Scheme != "WRAP")
            {
                Log.LogVerbose("Token has invalid scheme. Expected: WRAP");
                return false;
            }

            try
            {
                var token = ParseAccessToken(request.Headers.Authorization.Parameter);
                return this.validator.IsTokenValid(token);
            }
            catch (IndexOutOfRangeException)
            {
                Log.LogVerbose("{0} is an invalid authorization header value", request.Headers.Authorization.Parameter);
                return false;
            }
        }

        public static SwtAuthorization Create(AcsOptions acsOptions)
        {
            var validator = new SwtTokenValidator(acsOptions.Issuer, acsOptions.Audience, acsOptions.SigningKey);
            return new SwtAuthorization(validator);
        }

        private static string ParseAccessToken(string parameter)
        {
            const int OneCharacter = 1;
            var parts = parameter.Split('=');

            return parts[1]
                    .Remove(0, OneCharacter)
                    .Remove(parts[1].Length - 2, OneCharacter);
        }
    }
}
